	var stepNums=1;

	function addMainStep(){
		var mainStepBlock=$('#mainStepBlock').clone(true);
		var stepId="step_id_"+stepNums;
		$(mainStepBlock).attr("id",stepId);
		stepNums++;
//		$(mainStepBlock).find("[name='stepIndex']").html('步骤'+(stepNums++));
		$(mainStepBlock).find("[stepId]").attr("stepId", stepId);
		$(mainStepBlock).find("[name='fixed']").attr("id","main_task_fixed_"+stepId);
		$(mainStepBlock).find("[name='stepDes']").attr("id","main_task_name_"+stepId);
		
		$(mainStepBlock).find("#stepDelBtn").bind("click",function(){
			if(confirm("确认删除?")){
				$(mainStepBlock).remove();
				
				$(".stepIndex").each(function(i){
					$(this).html('步骤'+(i+1));
				});
			}
		});
		
		
		var taskList=$('#allTasks').clone(true);
		$(taskList).attr("id","main_task_select_"+stepId);
		$(taskList).bind('change',function(){
			stepMainTaskSelect(stepId);
		});
		$(mainStepBlock).find("[name='taskList']").append(taskList);
		$(mainStepBlock).addClass("stepClass");
		
		$('#stepBlock').append(mainStepBlock);
		$('#stepBlock').append('<div class="clear"></div>');
		
		$(".stepIndex").each(function(i){
			$(this).html('步骤'+(i+1));
		});
		
		return mainStepBlock;
	}
	
	function openCloseSubStep(stepId,status){
		var mainStepBlock=$('#'+stepId);
		var btnImg=$(mainStepBlock).find("#openBtn");
		
		if(status){
			$(btnImg).attr('status',status);
		}
		
		if($(btnImg).attr('status')=='close'){
			$(mainStepBlock).find("div[stepid='"+stepId+"']").fadeIn('fast');
			$(btnImg).attr('status','open');
			$(btnImg).attr('src','../images/tree_arrow_open.gif');
		}else{
			$(mainStepBlock).find("div[stepid='"+stepId+"']").fadeOut('fast');
			$(btnImg).attr('status','close');
			$(btnImg).attr('src','../images/tree_arrow_close.gif');
		}
	}
	
	
	function stepMainTaskSelect(stepId){
		var mainStepBlock=$('#'+stepId);
		var taskSelect=$(mainStepBlock).find('#'+"main_task_select_"+stepId);
		$(mainStepBlock).find("div[stepid='"+stepId+"']").empty();
		if(taskSelect[0].value==4){
			openCloseSubStep(stepId,'close');
			$(mainStepBlock).find("#openBtn").bind("click",function(){
				openCloseSubStep(stepId);
			});
			
			var option=$(taskSelect[0].options[taskSelect[0].selectedIndex]);
			var items=$(option).attr('taskItems').split(',');
			for(var i=0;i<items.length;i++){
				var item=$.trim(items[i]);
				if(item==''){
					continue;
				}
				addSubTask(stepId,item,i+1);
			}
			
		}else{
			openCloseSubStep(stepId,'open');
			$(mainStepBlock).find("#openBtn").unbind("click");
		}
	}
	
	function addSubTask(stepId,itemName,index){
		var subStepBlock=$('#subStepBlock').clone(true);
		var subStepBranchId=stepId+"_"+index;
		$(subStepBlock).attr("id",subStepBranchId);
		$(subStepBlock).find("[name='itemName']").html(itemName);
		$(subStepBlock).find("a[subStepBranchId]").attr("subStepBranchId",subStepBranchId);
		$(subStepBlock).find("a[subStepBranchId]").bind('click',function(){
			addSubBranchTask(stepId,subStepBranchId,$(this).attr('index'));
			$(this).attr('index',parseInt($(this).attr('index'))+1);
		});
		$(subStepBlock).addClass("stepItemsClass_"+stepId);
		
		$('#'+stepId).find("div[stepid='"+stepId+"']").append(subStepBlock);
		
		return subStepBlock;
	}
	
	function addSubBranchTask(stepId,subStepBranchId,index){
		
		var stepIndex=stepId.replace(/step_id_/,'');
		var subBranchTask=$('#subStepBranch').clone(true);
		var subBranchTaskId=subStepBranchId+"_"+index;
		
		$(subBranchTask).attr("id",subBranchTaskId);
		$(subBranchTask).find("[name='stepIndex']").html('步骤 '+stepIndex+"."+index);
		
		var taskList=$('#allTasks').clone(true);
		$(taskList).attr("id","sub_branch_task_select_"+subBranchTaskId);
		$(taskList).bind('change',function(){
			subStepBranchTaskSelect(subBranchTaskId);
		});
		
		$(subBranchTask).find("[name='taskList']").append(taskList);
		
		$(subBranchTask).addClass("stepitemBranch_"+subStepBranchId);
		$(subBranchTask).find("[name='stepDes']").attr("id","stepitemBranch_name_"+subBranchTaskId);
		
		
		$('#'+subStepBranchId).append(subBranchTask);
		$('#'+subStepBranchId).append('<div class="clear"></div>');
		
		return subBranchTask;
	}
	
	function subStepBranchTaskSelect(subBranchTaskId){
		var select=$('#sub_branch_task_select_'+subBranchTaskId);
		$('#'+subBranchTaskId).find('#lastSpan').empty();
		if(select[0].value==4){
			var option=$(select[0].options[select[0].selectedIndex]);
			var items=$(option).attr('taskItems').split(',');
			for(var i=0;i<items.length;i++){
				var item=$.trim(items[i]);
				if(item==''){
					continue;
				}
				addLastTask(subBranchTaskId,item,i+1);
			}
		}
	}
	
	function addLastTask(subBranchTaskId,item,index){
		var subStepBlock=$('#subStepBlock').clone(true);
		$(subStepBlock).attr("id",subBranchTaskId+"_"+index);
		$(subStepBlock).find("[name='itemName']").html(item);
		$(subStepBlock).find("a[subStepBranchId]").attr("subStepBranchId",subBranchTaskId+"_"+index);
		$(subStepBlock).find("a[subStepBranchId]").bind('click',function(){
			addLastStepTask(subBranchTaskId+"_"+index,$(this).attr('index'));
			$(this).attr('index',parseInt($(this).attr('index'))+1);
		});
		
		$(subStepBlock).addClass("subStepClass_"+subBranchTaskId);
		$('#'+subBranchTaskId).find('#lastSpan').append(subStepBlock);
		
		return subStepBlock;
	}
	
	function addLastStepTask(id,index){
		var secSubStep=$('#secSubStep').clone(true);
		var minTask=$('#minTasks').clone(true);
		$(minTask).attr("id","lastStep_select"+id+"_"+index);
		$(secSubStep).attr("id",id+"_"+index);
		
		var stepIndex=id.replace(/step_id_/,'');
		var t=stepIndex.split("_");
		
		$(secSubStep).find('[name="stepName"]').html('步骤'+t[0]+"."+t[2]+"."+index);
		$(secSubStep).find('[name="taskList"]').append(minTask);
		$(secSubStep).addClass("lastStepClass_"+id);
		$(secSubStep).find("[name='stepDes']").attr("id","lastStep_name"+id+"_"+index);
		
		$('#'+id).append(secSubStep);
		$('#'+id).append('<div class="clear"></div>');
		
		return secSubStep;
	}
	
	
	
	function checkNull(){
		assignNameForNull();
		return $("#newJobForm").valid();	
	}
	
	function findTaskById(taskId,allTask){
		for(var i=0;i<allTask.length;i++){
			if(allTask[i].id==(taskId)){
				return allTask[i];
			}
		}
	}
	
	/**
	 * @param {} allTask
	 */
	function submitAddJob(allTask){
		if(!checkNull()){
			return;
		}
		
		var job=new Job();
		$('.stepClass').each(function(){
			var stepElement=$(this);
			var stepObj=createStep(job,stepElement);
			if(stepObj.task.actionId==TaskAction.SELECT_SINGLE){
				stepObj.createStepItems();
				createStepItemsData(stepObj,stepElement);
			}
		});
		job.name=$('#jobName').val();
		if(document.getElementById("jobId")){
			job.id=$('#jobId').val();
		}
		
		if(document.getElementById("jobVersion")){
			job.version=$('#jobVersion').val();
		}
		
		
		
//		alert(JSON.stringify(job));
//		return;
		
    	$.ajax(
    		{
		    	type :'POST',
		    	url : 'newJob.html', 
		        data: JSON.stringify(job),   
		        dataType: 'text',  
		        contentType:'application/json;charset=UTF-8',
		        success:function(dataTxt){
		        	var data=JSON.parse(dataTxt);
		        	if(data.success){
		        		alert('任务保存成功!');
		        		window.location='jobList.html';
		        	}else{
		        		alert(data.msgBody);
		        	}
		        }
    		}
        );   
	}
	
	function createStep(job,stepElement){
		var id = stepElement.attr("id");
		var taskSelect = $('#main_task_select_' + id);
		var option = $(taskSelect[0].options[taskSelect[0].selectedIndex]);
		var taskObj = findTaskById($(option).attr('taskId'), allTask);
		var fixed = $('#main_task_fixed_' + id).attr("checked") ? true : false;
		var stepName = $('#main_task_name_' + id).val();
		var checkBox=$(stepElement).find('#checkBox').attr("checked")?1:0;
		
		stepObj=job.createStep(taskObj, stepName, fixed);
		stepObj.checkBox=checkBox;
		
		if(stepObj.task.actionId==TaskAction.INPUT_MULTIPLE_SCORE){
			var taskItems=$(option).attr('taskItems');
			var taskItemsObj=JSON.parse(taskItems);
			
			//alert(stepObj.task.taskItems.length);
			
			for(var i=0;i<taskItemsObj.length;i++){
				stepObj.task.taskItems.push(new TaskItem({
					name:taskItemsObj[i].name,
			        extra1:taskItemsObj[i].extra1,
			        extra2:taskItemsObj[i].extra2,
			        extra3:taskItemsObj[i].extra3
				}))
			}
		}
		
		return stepObj;
	}
	
	function createStepItemsData(step,stepElement){
		var id = stepElement.attr("id");
		$('.stepItemsClass_'+id).each(function(i){
			createStepForStepItem(step.stepItems[i],$(this));
		});
		
	}
	
	function createStepForStepItem(stepItemObj,stepItemElement){
		var id = stepItemElement.attr("id");
		$('.stepitemBranch_'+id).each(function(i){
			var stepName=$('#stepitemBranch_name_'+$(this).attr("id")).val();
			var taskSelect=$('#sub_branch_task_select_'+$(this).attr("id"));
			var option = $(taskSelect[0].options[taskSelect[0].selectedIndex]);
			var taskObj = findTaskById($(option).attr('taskId'), allTask);
			var stepObj=stepItemObj.createStep(taskObj,stepName);
			
			if(stepObj.task.actionId==TaskAction.SELECT_SINGLE){
				$('.subStepClass_'+$(this).attr("id")).each(function(i){
					createStepItems(i,stepObj,$(this));
				});
			}	
			
			if(stepObj.task.actionId==TaskAction.INPUT_MULTIPLE_SCORE){
				var taskItems=$(option).attr('taskItems');
				var taskItemsObj=JSON.parse(taskItems);
				
				for(var i=0;i<taskItemsObj.length;i++){
					stepObj.task.taskItems.push(new TaskItem({
						name:taskItemsObj[i].name,
				        extra1:taskItemsObj[i].extra1,
				        extra2:taskItemsObj[i].extra2,
				        extra3:taskItemsObj[i].extra3
					}))
				}
			}
			
			
			
		});
	}
	
	function createStepItems(index,stepObj,element){
		var id = element.attr("id");
		
		//alert(stepObj.stepItems.length);
		
		$('.lastStepClass_'+id).each(function(i){
			var stepName=$('#lastStep_name'+$(this).attr("id")).val();
			var taskSelect=$('#lastStep_select'+$(this).attr("id"));
			var option = $(taskSelect[0].options[taskSelect[0].selectedIndex]);
			var taskObj = findTaskById($(option).attr('taskId'), allTask);
			
			
			var stepInstance=stepObj.stepItems[index].createStep(taskObj,stepName);
			
			
//			if(stepInstance.task.actionId==TaskAction.INPUT_MULTIPLE_SCORE){
//				
//				var taskItems=$(option).attr('taskItems');
//				var taskItemsObj=JSON.parse(taskItems);
//				
//				
//				for(var i=0;i<taskItemsObj.length;i++){
//					stepInstance.task.taskItems.push(new TaskItem({
//						name:taskItemsObj[i].name,
//				        extra1:taskItemsObj[i].extra1,
//				        extra2:taskItemsObj[i].extra2,
//				        extra3:taskItemsObj[i].extra3
//					}))
//				}
//			}
			
			
		});
		
	}
	
	
	
	
